layui.use(['layer', 'form', 'table', 'ztree', 'laydate', 'admin', 'ax'], function () {
    var $ = layui.$;
    var table = layui.table;
    var $ax = layui.ax;
    var admin = layui.admin;

    var layer = layui.layer;
    var $ZTree = layui.ztree;
    var laydate = layui.laydate;

    /**
     * 商品信息表管理
     */
    var HxBasicGoods = {
        tableId: "hxBasicGoodsTable",    //表格id
        condition: {
            goodsBigtypedictId: "",
        }
    };

    /**
     * 初始化表格的列
     */
    HxBasicGoods.initColumn = function () {
        return [[
            {type: 'checkbox'},
            {field: 'id', hide: true, title: '主键'},
            {align: 'center', toolbar: '#tableBar', title: '图片', width: 60},
            {field: 'goodsName', sort: true, title: '名称', width: 160},
            {field: 'goodsNumber', sort: true, title: '编号'},
            {field: 'goodsPcode69', sort: true, title: '商品码'},
            {field: 'goodsSpecs', sort: true, title: '规格'},
            {field: 'jibenUnit', sort: true, title: '基本单位'},
            {field: 'goodsWarranty', sort: true, title: '保质期'},
        ]];
    };

    /**
     * 选择时
     */
    HxBasicGoods.onClickDept = function (e, treeId, treeNode) {
        HxBasicGoods.condition.goodsBigtypedictId = treeNode.id;
        HxBasicGoods.search();
    };

    /**
     * 点击查询按钮
     */
    HxBasicGoods.search = function () {
        var queryData = {};
        queryData['goodsCustomerId'] = $("#goodsCustomerId").val();
        queryData['condition'] = $("#condition").val();
        queryData['goodsBigtypedictId'] = HxBasicGoods.condition.goodsBigtypedictId;
        table.reload(HxBasicGoods.tableId, {
            where: queryData, page: {curr: 1},
             done:function () {
            }
        });
    };

    // 渲染表格
    var tableResult = table.render({
        elem: '#' + HxBasicGoods.tableId,
        url: Feng.ctxPath + '/hxBasicGoods/list',
        page: true,
        limit: 40,
        height: "full-60",
        cellMinWidth: 100,
        cols: HxBasicGoods.initColumn()
    });
    
    $('#assign').click(function () {
    	var checkStatus = table.checkStatus('hxBasicGoodsTable');
        data = checkStatus.data;
        if (data.length>0) {
            var ids = '';
            var goodsNames = '';
            for (var i = 0; i < data.length; i++) {
            	if(i > 0){
            		ids+=",";
            		goodsNames+=",";
            	}
            	ids+=data[i].id;
            	goodsNames+=data[i].goodsName;
			}
	    	parent.HxBasicGoodsInfoDlg.data.id = ids;
	    	parent.HxBasicGoodsInfoDlg.data.goodsName = goodsNames;//商品编号
	    	var Index = parent.layer.getFrameIndex(window.name);
	        parent.layer.close(Index);
		} else {
			layer.alert('请至少选中一条数据');
		}
    });
    
    //回填商品数据
    function confirm(data){
    	parent.HxBasicGoodsInfoDlg.data.id = data.id;
    	parent.HxBasicGoodsInfoDlg.data.goodsName = data.goodsName;//商品名称
    	parent.HxBasicGoodsInfoDlg.data.goodsNumber = data.goodsNumber;
    	parent.HxBasicGoodsInfoDlg.data.goodsPcode69 = data.goodsPcode69;
    	parent.HxBasicGoodsInfoDlg.data.goodsSpecs = data.goodsSpecs;

    	parent.HxBasicGoodsInfoDlg.data.kcUnit = data.kcUnit;
    	parent.HxBasicGoodsInfoDlg.data.kcRate = data.kcRate;
    	parent.HxBasicGoodsInfoDlg.data.cgUnit = data.cgUnit;
    	parent.HxBasicGoodsInfoDlg.data.cgRate = data.cgRate;
    	parent.HxBasicGoodsInfoDlg.data.pfUnit = data.pfUnit;
    	parent.HxBasicGoodsInfoDlg.data.pfRate = data.pfRate;
    	parent.HxBasicGoodsInfoDlg.data.psUnit = data.psUnit;
    	parent.HxBasicGoodsInfoDlg.data.psRate = data.psRate;

    	parent.HxBasicGoodsInfoDlg.data.goodsVolume = data.goodsVolume;
    	parent.HxBasicGoodsInfoDlg.data.goodsWeight = data.goodsWeight;
    	parent.HxBasicGoodsInfoDlg.data.goodsChargingCg = data.goodsChargingCg;
    	parent.HxBasicGoodsInfoDlg.data.goodsChargingPs = data.goodsChargingPs;
    	parent.HxBasicGoodsInfoDlg.data.goodsChargingPf = data.goodsChargingPf;
    	var Index = parent.layer.getFrameIndex(window.name);
        parent.layer.close(Index);
    }
    
    // 双击行事件
    table.on('rowDouble(' + HxBasicGoods.tableId + ')', function (obj) {
        var data = obj.data;
    	confirm(data);
    });

    // 工具条点击事件
    table.on('tool(' + HxBasicGoods.tableId + ')', function (obj) {
        var data = obj.data;
        if (obj.event === 'paymentProve') {
  	    	//构建图像数组
      		var items = [];
        	if (data.v1!='') {
        		var obj = {};
        		obj.src = '/system/previewa/' + data.v1;
        		obj.title = '预览图片';
        		items.push(obj)
    	      	//定义选项（如果需要）
    	      	var options = {
    	      	    index: 0 // 此选项表示您将从第一张图片开始
    	      	};
    	      	//初始化插件
    	      	var viewer = new PhotoViewer(items, options);
			} else {
                Feng.info("暂无照片!");
			}
        } 
    });

    //初始化左侧部门树
    var ztree = new $ZTree("deptTree", "/hxBasicGoodsCode/treeo");
    ztree.bindOnClick(HxBasicGoods.onClickDept);
    ztree.init();

    // 搜索按钮点击事件
    $('#btnSearch').click(function () {
        HxBasicGoods.search();
    });

    //返回按钮
    $("#backupPage").click(function () {
    	var Index = parent.layer.getFrameIndex(window.name);
        parent.layer.close(Index);
    });

});
